|
|
|
|
|
|
|
You can use the UNIQUE CONSTRAINT clause in a multicolumn index. This is especially handy if you have a data table containing more than one field that must be evaluated when deciding uniqueness. For example, what if the preceding data table, in addition to BudgetCode, had BudgetPrefix and BudgetSuffix, too? You can make sure that the combination of the three fields is always unique by building a multicolumn CONSTRAINT clause. Use the code example in Listing 13.11 as a guide. Update your SQLVB04.SQV script to match the example in Listing 13.11 and execute it to make sure you have written the syntax correctly. |
|
|
|
|
|
|
|
|
Listing 13.11. Defining a multicolumn UNIQUE CONSTRAINT. |
|
|
|
|
|
|
|
|
//
// sqlvb04.sqv - Test CONSTRAINT SQL keyword
//
// create a database
dbMake sqlvb04.mdb;
// create jobs title table
CREATE TABLE JobsTable
(JobTitle TEXT (20) CONSTRAINT PKJobTitle PRIMARY KEY,
BudgetPrefix TEXT(5),
BudgetCode TEXT(10),
BudgetSuffix TEXT(5),
JobDesc MEMO,
CONSTRAINT UKBudget UNIQUE (BudgetPrefix,BudgetCode,BudgetSuffix)
);
// show table
SELECT * FROM JobsTable;
// eof |
|
|
|
|
|
|
|
|
Once the script has executed, your screen should look similar to the one in Figure 13.14. |
|
|
|
|
|
|
|
|
Figure 13.14.
The results of a multicolumn CONSTRAINT clause. |
|
|
|
|
|
|
|
|
You should also be aware of an important difference between the single-column and multicolumn CONSTRAINT clause formats. Notice that when you are defining a single-column CONSTRAINT, you place the CONSTRAINT clause directly after the column definition without a comma between the column type and the CONSTRAINT keyword. In the multicolumn |
|
|
|
|
|